//utf-8
//点击新增按钮 事件跳转至 新增页面
function toAddData(){
	var dialogStruct={
		'display':contextRootPath+'/tCMmLoanDetail/prepareExecute/toAdd',
		'width':800,
		'height':500,
		'title':'新增',
		'isIframe':'false',
		'buttons':[
         {'text':'保存','action':doAddFrom,'isClose':true},
		 {'text':'关闭','isClose':true}
		]
	};
	
	var dialogAdd =jyDialog(dialogStruct).open();
}
//新增页面的保存操作
function doAddFrom(){
	//序列化 新增页面的form表单数据
	var params=$("#addNewsFormData").serialize();
	var url=contextRootPath+'/tCMmLoanDetail/insertTCMmLoanDetail';
	//通过ajax保存
	jyAjax(
		url,
		params,
		function(msg){
			//新增成功后，
			alert(msg.msg);
			var v_status = msg.status;
        	if(v_status.indexOf('ok') >-1){
        		//新增成功后 刷新页面 或 只查询 id为msg.data['id'] 的  数据
        		queryData();
        	}
  	});
}
//修改 事件
var dialogUpdate;
var selectids = null;
function toUpdateData(){
	var v_ids = iframe.iframeObj["table"].getSelectedIds().join(",");
	var v_loanStatus = iframe.iframeObj["table"].getSelectedObjs("loanStatus").join(",");
	//如果没有选中 数据则
	if(v_ids == ""){
		alert("请选择待修改的数据！");
		return;
	}
	//如果选择多个 择提示
	if(v_ids.indexOf(",") > -1){
		alert("请选择一条数据！");
		return;
	}
	
//	if(v_loanStatus!='01'){
//		 alert("只要贷款状态为“在途”可以修改。");
//	     return;
//	}
	
	selectids = iframe.iframeObj["table"].getSelectedObjs("id");
	var dialogStruct={
			'display':contextRootPath+'/loanDetail/prepareExecute/toUpdate?id='+v_ids,
			'width':1200,
			'height':800,
			'title':'修改',
			'isIframe':'false',
			'buttons':[
             {'text':'保存','action':doUpdateFrom,'isClose':true},
			 {'text':'关闭','isClose':true}
			]
		};
		
	dialogUpdate = jyDialog(dialogStruct).open();
}
//修改页面 的 保存操作
function doUpdateFrom(){
	//序列化 新增页面的form表单数据
	var params=$("#updateNewsFormData").serialize();
	var dataUrl=contextRootPath+'/loanDetail/updateLoanDetail';
	//通过ajax保存

	//通过ajax保存
	jyAjax(
		dataUrl,
		params,
		function(msg){
			//保存成功后，执行查询页面查询方法
			alert(msg.msg);
      	var v_status = msg.status;
      	if(v_status.indexOf('ok') >-1){
      		//新增成功后 刷新页面 或 只查询 id为msg.data['id'] 的  数据
      		//dialogBatchUpdate.close();
      		queryData();
      	}
	});
    

}
//导出贷款信息exportForm
function exportForm(){
	var contractNo=$("input[name=contractNo]").val();
	var custCode=$("input[name=custCode]").val();
	var dealCode=$("input[name=dealCode]").val();
	var creditPackageName=$("input[name=creditPackageName]").val();
	var cooperorgCode=$("#cooperorgCode").val();
	var contractCode=$("#contractCode").val();
	var agreementCode=$("#agreementCode").val();
	var loanStatus=$("select[name=loanStatus]").val();
	var abnormalStatus="";//$("select[name=abnormalStatus]").val();
	var loanEachStatus=$("select[name=loanEachStatus]").val();
	if(null==cooperorgCode ||""==cooperorgCode){
		alert("请选择合作机构！");
		return;
	}
	var dataUrl = contextRootPath + '/tCMmLoanDetail/exportLoanDetail?contractNo=' + contractNo+ "&custCode="+custCode+"&dealCode="+dealCode+ "&creditPackageName="+creditPackageName+ "&cooperorgCode="+cooperorgCode+ "&contractCode="+contractCode+"&agreementCode="+agreementCode+"&loanStatus="+loanStatus+"&abnormalStatus="+abnormalStatus+"&loanEachStatus="+loanEachStatus;
	//var dataUrl = contextRootPath + '/tCMmLoanDetail/exportLoanDetail?contractNo=' + contractNo+ "&custCode="+custCode+ "&dealCode="+dealCode+ "&creditPackageName="+creditPackageName+ "&cooperorgCode="+cooperorgCode+ "&contractCode="+contractCode"&agreementCode="+agreementCode"&loanStatus="+loanStatus"&abnormalStatus="+abnormalStatus;
	window.location = dataUrl;

}

//铜板街紧急扣回操作
function updateLoanDetailData(dataUrl){
	
	var params;	
	if(confirm("确认紧急扣回吗？")){
		//通过ajax保存
		jyAjax(
			dataUrl,
			params,
			function(msg){
				//保存成功后，执行查询页面查询方法
				alert(msg.msg);
	      	var v_status = msg.status;
	      	if(v_status.indexOf('ok') >-1){
	      		//新增成功后 刷新页面 或 只查询 id为msg.data['id'] 的  数据
	      		//dialogBatchUpdate.close();
	      		queryData();
	      	}
		});
	}
	
	
	
}

//验证是否推送
function isPropellingMovement(){
	var params;	
	var ids = iframe.iframeObj["table"].getSelectedIds().join(",");
	var loanStatus = iframe.iframeObj["table"].getSelectedObjs("loanStatus").join(",");
	var quotaCode = iframe.iframeObj["table"].getSelectedObjs("quotaCode").join(",");
	var contractNo = iframe.iframeObj["table"].getSelectedObjs("contractNo").join(",");
	var dealLoanStatus = '03';
	
	//如果没有选中 数据则
	if(ids == ""){
		alert("请选择待修改的数据！");
		return;
	}
	//如果选择多个 择提示
	if(ids.indexOf(",") > -1){
		alert("请选择一条数据！");
		return;
	}
	
	if(loanStatus!='01'&& loanStatus!='02' && loanStatus!='00'){
		alert("只有“在途”、“审批通过”和“未使用”的可以紧急扣回！");
		return;
	}
	
	var dataUrl=contextRootPath+'/loanDetail/updateRiskLoanDetail?loanStatus=' + dealLoanStatus + "&selectids="+ids+"&contractNo="+contractNo;
	
	if(loanStatus =='00'){
		dataUrl = contextRootPath + '/tCMmQuota/deleteTCMmQuotaDetail?quotaCode=' + quotaCode + '&contractNo=' + contractNo;
	}
	
	var url = contextRootPath + "/tCMmLoanDetail/isPropellingMovement?contractNo=" + contractNo;
	jyAjax(
		url,
		params,
		function(msg){
			if(msg.loanEachStatus==cimsConstant.LOAN_EACH_STATUS_TBJ_SENDING || msg.loanEachStatus==cimsConstant.LOAN_EACH_STATUS_TBJ_SENDED){
				alert("待推送和推送完成的不能紧急扣回！");
				return;
			}else{
				updateLoanDetailData(dataUrl);
			}
	});
}

function isPropellingMovementPlus(){
	var ids = iframe.iframeObj["table"].getSelectedIds().join(",");
	var contractNo = iframe.iframeObj["table"].getSelectedObjs("contractNo").join(",");
	//如果没有选中 数据则
	if(ids == ""){
		alert("请选择待修改的数据！");
		return;
	}
	//如果选择多个 择提示
	if(ids.indexOf(",") > -1){
		alert("请选择一条数据！");
		return;
	}
	if(confirm("您确认要紧急扣回吗？")){
        $.ajax({
            type:"POST",
            dataType : "json",
            url:contextRootPath+"/loanDetail/updateRiskLoanDetail?contractNo="+contractNo,
            success:function(msg){
            	alert(msg.msg);
            	var v_status = msg.status;
            	//扣会成功之后
            	if(v_status.indexOf('ok') >-1){
            		queryData();
            	}
            	
            	
            },error: function(result) {
				alert("请查看返回数据是否正确：" + result.msg);
			}
        });
    }
}

//批量修改 事件

var dialogBatchUpdate;
function toBatchUpdateData(){
	var v_loanStatus = iframe.iframeObj["table"].getSelectedObjs("loanStatus");
	select_ids = iframe.iframeObj["table"].getSelectedObjs("id");
	//如果没有选中 数据则
	if(select_ids == ""){
		alert("请选择待修改的数据！");
		return;
	}
	var loanStatus = v_loanStatus[0];
	var checkflag1 = 0;
	var checkflag2 = 0;
	 	$.each(v_loanStatus,function(key){
	    	   if(v_loanStatus[key]!=loanStatus){
	    		   checkflag1 += 1;
	    	   }
	    	   
	    	   if(v_loanStatus[key]!='01'){
	    		   checkflag2 += 1;
	    	   }
	    	});
	 	
	    if(checkflag1>0){
	    	alert("选中的贷款状态不一致,请重新选择");
	    	return;
	    }
	    
	    if(checkflag2 > 0){
			 alert("只要贷款状态为“在途”可以批量修改。");
		     return;
	    }

	var dialogStruct={
			'display':contextRootPath+'/loanDetail/prepareExecute/toBatchUpdate?loanStatus='+loanStatus,
			'width':900,
			'height':300,
			'title':'批量修改',
			'isIframe':'false',
			'buttons':[
           {'text':'提交','action':doBatchUpdateFrom,'isClose':true},
			 {'text':'关闭','isClose':true}
			]
		};
		
	     dialogBatchUpdate = jyDialog(dialogStruct);
	     dialogBatchUpdate.open();
		
	
}

//修改页面 的 保存操作
function doBatchUpdateFrom(dialogBatchUpdate){
	//序列化 新增页面的form表单数据
	var params;
	//var loanStatus=document.getElementById("updateloanStatus").value;
	//var url=contextRootPath+'/loanDetail/batchupdateLoanDetail?loanStatus='+loanStatus+'&ids='+select_ids;
	//目前贷款状态 zhoup
	var loanStatusNow=document.getElementById("dtoloanStatus").value;
	//目标贷款状态 zhoup
	var loanStatus=document.getElementById("updateloanStatus").value;
	var url=contextRootPath+'/loanDetail/batchupdateLoanDetail?loanStatus='+loanStatus+'&ids='+select_ids;
	//当期贷款状态
	if(loanStatusNow == ""){
		alert("请选择当期贷款状态 ！");
		return;
	}
	//目标贷款状态 为空则
	if(loanStatus == ""){
		alert("请选择目标贷款状态 ！");
		return;
	}
	
	//通过ajax保存
	jyAjax(
		url,
		params,
		function(msg){
			//保存成功后，执行查询页面查询方法
			alert(msg.msg);
      	var v_status = msg.status;
      	if(v_status.indexOf('ok') >-1){
      		//新增成功后 刷新页面 或 只查询 id为msg.data['id'] 的  数据
      		//dialogBatchUpdate.close();
      		queryData();
      	}
	});
}






//删除 事件
function deleteData(){
	var v_ids = iframe.iframeObj["table"].getSelectedIds().join(",");
	//如果没有选中 数据则
	if(v_ids == ""){
		alert("请选择待删除的数据！");
		return;
	}
	if(confirm("您确认要删除选择的数据吗？")){
        $.ajax({
            type:"POST",
            dataType : "json",
            url:contextRootPath+"/tCMmLoanDetail/deleteTCMmLoanDetail?ids="+v_ids,
            success:function(msg){
            	alert(msg.msg);
            	var v_status = msg.status;
            	//删除成功后
            	if(v_status.indexOf('ok') >-1){
            		iframe.iframeObj["table"].removeSelectedRow();
            	}
            	
            	
            }
        });
    }
}
function getTbjStatus(contractNo,type){
    $.ajax({
        type:"POST",
        dataType : "json",
        url:contextRootPath+"/tCMmLoanDetail/getTbjLoanDetailStatus?contractNo="+contractNo+"&type="+type,
        success:function(msg){
        	art.dialog({title: '返回结果', content: msg.msg, ok: function () {return true; }, lock:true, background:'#600', opacity: 0.10});
        }
    });
}
//查看明细
function viewData(vId){
	var dialogStruct={
			'display':contextRootPath+'/tCMmLoanDetail/prepareExecute/toView?id='+vId,
			'width':800,
			'height':500,
			'title':'查看明细',
			'isIframe':'false',
			'buttons':[
			 {'text':'关闭','isClose':true}
			]
	};
		
	var dialogView = jyDialog(dialogStruct).open();
}